package com.yaoran.userauth.controller;

import com.yaoran.userauth.entity.Application;
import com.yaoran.userauth.service.ApplicationService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "应用管理")
@RestController
@RequestMapping("/application")
public class ApplicationController {
    private final static Logger logger = LoggerFactory.getLogger(ApplicationController.class);
    @Autowired
    private ApplicationService applicationService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public R applicationAdd(@RequestBody Application application){
        applicationService.addApplication(application,null);
        return R.ok();
    }

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public R<PageUtils<Application>> listApplication(@RequestBody Map<String,String> params){
        Map<String,Object> query = new HashMap<>();
        query.put("page",params.get("page"));
        query.put("limit",params.get("limit"));
        String keyWord = params.get("keyWord");
        PageUtils<Application> pageUtils = applicationService.listApplication(new Query(query),keyWord);
        return R.ok(pageUtils);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R updateApplication(@RequestBody Application application){
        applicationService.updateApplication(application);
        return R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/del")
    public R deleteApplication(@RequestBody Map<String,Long> params){
        applicationService.deleteApplication(params.get("id"));
        return R.ok();
    }

    @ApiOperation("批量删除")
    @PostMapping("/dels")
    public R deleteManyApplication(@RequestBody Map<String,Long[]> params){
        applicationService.deleteManyApplication(params.get("ids"));
        return R.ok();
    }

    @ApiOperation("应用详情")
    @GetMapping("/detail")
    public R<Application> getApplication(@RequestParam Long id){
        Application application = applicationService.getApplication(id);
        return R.ok(application);
    }

    @ApiOperation("应用列表，不分页")
    @GetMapping("/listAll")
    public R<List<Application>> listAllApplication(@RequestParam(value = "organizationId",required = false) Long organizationId){
        List<Application> application = applicationService.listAllApplication(organizationId);
        return R.ok(application);
    }
}
